<?php


require_once($WSC->get_site_inc_dir().'modules/jabba_ajax_grid_ctrl/jabba_ajax_grid_ctrl.class.php');
require_once($WSC->get_site_inc_dir().'modules/admin_panel/classes/admin_user/admin_user_dbc.class.php');


abstract class Site_Admin_Ajax_Grid_Ctrl_UA extends Jabba_Ajax_Grid_Ctrl {
	private $ua_config;
	protected $user;

	function __construct($tpl_file = '', $ua_config) {
		parent::__construct($tpl_file);

		$this->set_ua_config($ua_config);
	}


	public function set_ua_config($ua_config) {
		$this->ua_config = $ua_config;
	}


	public function get_ua_config() {
		return $this->ua_config;
	}


	public function _run() {
		$user_auth_config = $this->get_ua_config();

		$svm = $this->get_svm();

		if ($svm->is_global_var_registered($user_auth_config['session_var_name'])) {
			$user = $svm->get_global_var($user_auth_config['session_var_name']);

			if ($user instanceof User) {
				$this->user = $user;

				$_transit_vars_prepared = $this->prepare_transit_vars();

				$this->export('_transit_vars', $_transit_vars_prepared, true);

				$this->run();
			} else {
				$this->send_redirect_command();
			}
		} else {
			$this->send_redirect_command();
		}
	}


	public function get_user() {
		return $this->user;
	}


	private function send_redirect_command() {
		$user_auth_config = $this->get_ua_config();

		$redir_composer = new Redirect_Composer_Local($this->get__context(), $user_auth_config['login_page']);

		$context = $this->get__context();

		$location = $redir_composer->get_target_address($context);

		$rez = array('result_code' => Ajax_Ctrl::STATUS_OK, 'command' => Ajax_Ctrl::COMMAND_REDIRECT, 'command_value' => $location);

		$this->send_response($rez);
	}
}